# seckill_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class SeckillPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/seckill'
        self.product_name_locator = (By.ID, 'product-name')  # 产品名称定位器
        self.seckill_time_locator = (By.ID, 'seckill-time')  # 秒杀时间定位器
        self.add_to_cart_button_locator = (By.ID, 'add-to-cart-button')  # 添加到购物车按钮定位器
        self.cart_count_locator = (By.ID, 'cart-count')  # 购物车数量定位器
        self.seckill_status_locator = (By.ID, 'seckill-status')  # 秒杀状态定位器
        self.seckill_result_locator = (By.ID, 'seckill-result')  # 秒杀结果定位器
        self.error_message_locator = (By.ID, 'error-message')  # 错误消息定位器

    # 导航到秒杀页面
    def navigate(self):
        self.driver.get(self.url)

    # 获取正在秒杀的产品名称
    def get_product_name(self):
        return self.driver.find_element(*self.product_name_locator).text

    # 获取秒杀时间
    def get_seckill_time(self):
        return self.driver.find_element(*self.seckill_time_locator).text

    # 点击添加到购物车按钮
    def click_add_to_cart(self):
        self.driver.find_element(*self.add_to_cart_button_locator).click()

    # 获取当前购物车数量
    def get_cart_count(self):
        return int(self.driver.find_element(*self.cart_count_locator).text)

    # 等待秒杀状态更新
    def wait_for_seckill_status(self):
        WebDriverWait(self.driver, 10).until(
            EC.text_to_be_present_in_element(self.seckill_status_locator, "Seckill Active")
        )

    # 获取秒杀结果消息
    def get_seckill_result(self):
        return self.driver.find_element(*self.seckill_result_locator).text

    # 获取可能出现的错误消息
    def get_error_message(self):
        return self.driver.find_element(*self.error_message_locator).text